package com.apes.fn.controlling.vehicle.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.crm.customerExpand.FnCustomerExpand;
import com.apes.framework.jpa.ApesBean;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.tuple.date.DateTime;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.GenerationTime;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;

/**
 * 车辆服务信息
 *
 * @author xul
 * @create 2020-12-23 15:21
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = VehicleInfo.CONST_TABLE_NAME, indexes = {
        @Index(columnList = "dept_id, fn_customer_expand_id")
})
public class VehicleInfo implements ApesBean {
    public static final String CONST_TABLE_NAME = "vehicle_info";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "车辆服务信息")
    @Comment("编号")
    private String id;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "dept_id", referencedColumnName = "id", nullable = false)
    @Comment("门店")
    private Dept dept;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "fn_customer_expand_id", referencedColumnName = "id", nullable = false)
    @Comment("赋能客户车辆扩展信息")
    private FnCustomerExpand fnCustomerExpand;

    @States(name = "状态", values = {
            @State(id = "start" , name = "开始"),
            @State(id = "finish", name = "完成"),
            @State(id = "repeal", name = "终止")
    })
    @Column(name = "state", length = 30, nullable = false)
    private String state = "start";

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @DateTime(GenerationTime.INSERT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "create_date", updatable = false)
    @Comment("创建时间")
    private Date createDate;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @DateTime
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "update_date", insertable = false)
    @Comment("修改日期")
    private Date updateDate;

    @JSONField(format = DateUtil.SHOW_DATETIME_MILLI_FORMAT)
    @DateTime
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "last_updated_stamp")
    @Comment("时间戳")
    private Date lastUpdatedStamp;

    @Column(name = "pick_up_car", length = 120)
    @Comment("接车单号")
    private String pickUpCar;

    @Column(name = "q_test", nullable = false, columnDefinition = "number(1,0) default 0")
    @Comment("是否质检标记")
    private boolean qTest = false; //2021-03-23 弃用，改版了

    @Version
    @Column(name = "version", columnDefinition = "number(10) default 0")
    @Comment("版本号")
    private int version;  //JPA乐观锁

}
